<?php
namespace app\dsadmin\model;
use think\Model;
use think\Db;

class EnamecdnModel extends Model
{
    protected $name = 'enamecdn';

    public function getEnamesByWhere($map, $Nowpage, $limits)
    {
        return $this->where($map)->page($Nowpage, $limits)->order('id asc')->select();
    }
    
      public function insertEname($param)
    {
        
     
        try
        {
    		$for = explode("\n", $param['ename']);
			foreach($for as $vo)
			{
				$add[] = array('ename' => $param['ename'],'bename' => $param['bename'],'cname' => $param['cename'],'siname' => $param['siname'],'fename' => $param['fename'], 'status' => $param['status']);
			}
			$result = $this->insertAll($add);
            if (false === $result)
            {
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
            	return ['code' => 1, 'data' => '', 'msg' => '添加' . count($for) . '条域名成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    /**
     * 编辑管理员信息
     * 
     * @param  $param 
     */
    public function editEname($param)
    {
        try
        {
           
 
            $result = Db::name('enamecdn')->where(['id' => $param['id']])->update(['ename' => $param['ename'],'bename' => $param['bename']]);
            if (false === $result)
            {
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }
            else
            {
               
                return ['code' => 1, 'data' => '', 'msg' => '编辑域名成功'];
            }
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    public function delEname($id)
    {
        try
        {
        	$this->where('id', $id)->delete();
            Db::name('auth_group_access')->where('uid', $id)->delete();
            writelog(session('uid'), session('username'), '管理员【' . session('username') . '】删除域名成功(ID=' . $id . ')', 1);
            return ['code' => 1, 'data' => '', 'msg' => '删除域名成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

}
